{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Files with identical content (SHA-256 hash: 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92):\n",
      "  D:\\wsxwsx\\1.txt\n",
      "  D:\\wsxwsx\\2.txt\n",
      "\n",
      "Files with identical content (SHA-256 hash: a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3):\n",
      "  D:\\wsxwsx\\3.txt\n",
      "  D:\\wsxwsx\\4.txt\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import hashlib\n",
    "from collections import defaultdict\n",
    "\n",
    "def calculate_file_hash(file_path):\n",
    "    \"\"\"Calculate the SHA-256 hash of a file.\"\"\"\n",
    "    sha256_hash = hashlib.sha256()\n",
    "    with open(file_path, \"rb\") as f:\n",
    "        # Read and update hash in chunks of 4K\n",
    "        for byte_block in iter(lambda: f.read(4096), b\"\"):\n",
    "            sha256_hash.update(byte_block)\n",
    "    return sha256_hash.hexdigest()\n",
    "\n",
    "def find_duplicate_files(directory):\n",
    "    \"\"\"Find duplicate files in the specified directory and its subdirectories.\"\"\"\n",
    "    file_hashes = defaultdict(list)\n",
    "\n",
    "    for root, dirs, files in os.walk(directory):\n",
    "        for file in files:\n",
    "            file_path = os.path.join(root, file)\n",
    "            file_hash = calculate_file_hash(file_path)\n",
    "            file_hashes[file_hash].append(file_path)\n",
    "\n",
    "    # Filter out entries with only one file path (not duplicates)\n",
    "    duplicates = {hash_value: paths for hash_value, paths in file_hashes.items() if len(paths) > 1}\n",
    "    return duplicates\n",
    "\n",
    "def print_duplicates(duplicates):\n",
    "    \"\"\"Print the duplicate files with their paths.\"\"\"\n",
    "    for hash_value, paths in duplicates.items():\n",
    "        print(f\"Files with identical content (SHA-256 hash: {hash_value}):\")\n",
    "        for path in paths:\n",
    "            print(f\"  {path}\")\n",
    "        print()\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    directory_to_search = r\"D:\\wsxwsx\"  # Specify the directory to search\n",
    "    duplicates = find_duplicate_files(directory_to_search)\n",
    "    if duplicates:\n",
    "        print_duplicates(duplicates)\n",
    "    else:\n",
    "        print(\"No duplicate files found in the specified directory.\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
